package cn.edu.sdjzu.xg.bysj.controller.basic;

import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectType;
import cn.edu.sdjzu.xg.bysj.service.GraduateProjectTypeService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@Slf4j
@WebServlet("/graduateProjectType.ctl")
public class GraduateProjectTypeController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            //分页获得满足条件的所有教师
            Collection<GraduateProjectType> graduateProjectTypes =
                    GraduateProjectTypeService.getInstance().findAll();
            //把获取来的集合对象转化成json字串
            String graduateProjectTypes_jsonStr = JSON.toJSONString(graduateProjectTypes);
            //响应resp_jsonObj到前端
            response.getWriter().println(graduateProjectTypes_jsonStr);
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            log.error(e.getMessage(), e);
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试（要用log4j代替）

        } catch (Exception e) {
            log.debug(e.getMessage());
            respMessage_jsonObj.put("message", "网络异常");
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage(), e);
        }
    }
}